/**
 * 医嘱执行记录管理初始化
 */
var CdcOrdexec = {
    id: "CdcOrdexecTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CdcOrdexec.initColumn = function () {
    return [
        {title: '序号',width:50,formatter: function (value, row, index) {return index+1;}},
        {title: '医嘱项',width:200, field: 'orderMast.description', align: 'center', valign: 'middle', sortable: true},
        {title: '状态',width:100, field: 'busOrderStatus.description', align: 'center', valign: 'middle', sortable: true},
        {title: '开医嘱日期',width:100, field: 'orderDate', align: 'center', valign: 'middle', sortable: true},
        {title: '开医嘱科室',width:100, field: 'orderLoc.description', align: 'center', valign: 'middle', sortable: true},
        {title: '接收科室',width:100, field: 'recLoc.description', align: 'center', valign: 'middle', sortable: true},
        {title: '开始日期',width:100, field: 'startDate', align: 'center', valign: 'middle', sortable: true},
        {title: '停止日期',width:100, field: 'endDate', align: 'center', valign: 'middle', sortable: true},
        {title: '给药途径',width:100, field: 'busOrderInstruc.description', align: 'center', valign: 'middle', sortable: true},
        {title: '单次剂量',width:100, field: 'doseQty', align: 'center', valign: 'middle', sortable: true},
        {title: '剂量单位',width:100, field: 'busOrderUnit.description', align: 'center', valign: 'middle', sortable: true},
        {title: '频次',width:100, field: 'busOrderFreq.description', align: 'center', valign: 'middle', sortable: true},
        {title: '是否抗生素',width:100, field: 'isAnti', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
            return value?"是":"否";
        }},
        {title: '索引码',width:100, field: 'xcode', align: 'center', valign: 'middle', sortable: true},
        {title: '业务数据类型',width:100, field: 'data.description', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效',width:100, field: 'isActive', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
            return value?"是":"否";
        }}
    ];
};

/**
 * 检查是否选中
 */
CdcOrdexec.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CdcOrdexec.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增医嘱执行记录
 */
CdcOrdexec.add = function () {
    var index = Sys.open('新增医嘱执行记录',Sys.backendPath + '/cdc/cdcordexec/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看医嘱执行记录详情
 */
CdcOrdexec.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('医嘱执行记录详情',Sys.backendPath + '/cdc/cdcordexec/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除医嘱执行记录
 */
CdcOrdexec.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/cdcordexec/delete", function (data) {
	            Sys.success("删除成功!");
	            CdcOrdexec.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("cdcOrdexecId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除医嘱执行记录?",operation);
    }
};

/**
 * 查询医嘱执行记录列表
 */
CdcOrdexec.query = function () {
    if ((EpisodeIDTable.selEpisode)&&(EpisodeIDTable.selEpisode.id)) {
        var queryData=queryData = '{"NotQuery":"false","episodeId":"' + EpisodeIDTable.selEpisode.id + '"}';
        queryData = JSON.parse(queryData);
        CdcOrdexec.table.clear();
        CdcOrdexec.table.refresh({ query: queryData });
    }else{
        Sys.info('请先选择患者！' );
    }
};
$(function () {
    var defaultColunms = CdcOrdexec.initColumn();
    var table = new BSTable(CdcOrdexec.id, Sys.backendPath + "/cdc/cdcordexec/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(20);
    table.setShowRefresh(false);
    table.setShowColumns(false);
    table.setShowToggle(false);
    table.setShowExport(false);
    table.setSubHead(30);
    table.setQueryParams({ NotQuery: true });
    CdcOrdexec.table = table.init();
    CdcOrdexec.query();
});
